package translinkApp.Utilities;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import translinkApp.Data.Journey.Search.JourneySearchList;
import translinkApp.Data.ResolvedLocation.ResolvedLocationList;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Environment;

public class DataResource<T> {

	private final String name;
	private T data;

	public static DataResource<ResolvedLocationList> savedLocations;
	public static DataResource<JourneySearchList> journeysList;
	public static DataResource<JourneySearchList> journeysHistoryList;

	BroadcastReceiver mExternalStorageReceiver;
	boolean mExternalStorageAvailable = false;
	boolean mExternalStorageWriteable = false;

	static {

		savedLocations = new DataResource<ResolvedLocationList>("FavoriteLocations",
				new ResolvedLocationList(), null);

		journeysList = new DataResource<JourneySearchList>("SavedJourneys",
				new JourneySearchList(), null);

		journeysHistoryList = new DataResource<JourneySearchList>("JourneysHistory",
				new JourneySearchList(), null);

	}

	public DataResource(String name) {
		this.name = name;
	}

	public DataResource(String name, T defaultValue, Context context) {
		this.name = name;
		load(defaultValue, null);
	}

	void updateExternalStorageState() {
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else {
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}

	}

	public void save(Context context) {
		FileOutputStream fos;
		try {
			fos = context.openFileOutput(name, Context.MODE_PRIVATE);

			ObjectOutputStream outStream = new ObjectOutputStream(fos);

			outStream.writeObject(data);

			fos.close();
		} catch (Exception e) {
			if (context != null) {
				Utils.showAlertDialog(context, "There was a problem saving the data\n" + e.getMessage());
			}
		}
	}

	public T load(T defaultValue, Context context) {

		T data = null;
		try {

			if (context != null) {
				FileInputStream fis = context.openFileInput(name);

				// Next, create an object that can read from that file.
				ObjectInputStream inStream = new ObjectInputStream(fis);

				// Retrieve the Serializable object.
				data = (T) inStream.readObject();

				inStream.close();
			}

		} catch (FileNotFoundException e1) {
			this.data = defaultValue;
			save(null);
			return defaultValue;
		} catch (IOException e) {
			data = null;
		} catch (ClassNotFoundException e) {
			data = null;
		}

		if (data == null) {
			this.data = defaultValue;
			return defaultValue;
		}

		this.data = (T) data;
		return (T) data;

	}

	public T getData() {
		return data;
	}

}